package controller;

import java.awt.BorderLayout;
import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.crypto.spec.OAEPParameterSpec;
import javax.swing.JFileChooser;

import main.Run;

import org.omg.CORBA.Environment;

import view.RelationshipminingView;
import weka.classifiers.meta.FilteredClassifier;
import weka.core.Instances;
import weka.core.converters.CSVLoader;
import weka.gui.AttributeSelectionPanel;

public class RelationshipController {

	private RelationshipminingView m_View;// Khởi tạo view
	private DataProcessing dprocess;
	Instances data;

	public RelationshipController() {
		dprocess = new DataProcessing();

		m_View = new RelationshipminingView();

		m_View.addExitButonListener(new ExitButtonListener());// Add listener to
																// view
		m_View.addStartFButonListener(new StartButtonListener());
		m_View.addGetDataButonListener(new GetDataButtonListener());
		m_View.addOpenButonListener(new OpenButtonListener());
		m_View.addReloadButonListener(new ReloadButtonListener());
		m_View.addAttributeselectionListener(new AttributeSelectionListener());
		m_View.setVisible(true);// hiển thị view

	}

	class ExitButtonListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			System.exit(0);
		}

	}

	class StartButtonListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {

			ClassifierFunction func = null;
			try {
				func = new ClassifierFunction(m_View.getM_Instances(), m_View);
			} catch (IOException e2) {
				e2.printStackTrace();
			}
			switch (m_View.getComboBox().getSelectedIndex()) {
			case 0:
				try {
					func.SMOMethod();
				} catch (Exception e2) {
					e2.printStackTrace();
				}
				break;
			case 1:
				try {
					func.NBMethod();
				} catch (Exception e2) {
					e2.printStackTrace();
				}
				break;
			case 2:
				try {
					func.C45Method();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				break;
			}
		}

	}

	class GetDataButtonListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {

			try {
				m_View.setEnableGetDataButton(false);
				dprocess.readDataBase();
				try {
					dprocess.process();
					// m_View.showMessage("Done");
					dprocess.exportStatistic();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				// load CSV
				CSVLoader loader = new CSVLoader();
				loader.setSource(new File("output.csv"));
				data = loader.getDataSet();

				// setting class attribute
				data.setClassIndex(data.numAttributes() - 1);
				// fc = new FilteredClassifier();

				m_View.setInstances(data);// Set instances to relationshipview
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}

	}

	class OpenButtonListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// String path =
			// getClass().getClassLoader().getParent().getResource(".").getPath();
			// // path to the directory to be opened
			// File file = new File(path);
			// Desktop desktop = null;
			// // Before more Desktop API is used, first check
			// // whether the API is supported by this particular
			// // virtual machine (VM) on this particular host.
			// if (Desktop.isDesktopSupported()) {
			// desktop = Desktop.getDesktop();
			// }
			// try {
			// desktop.open(file);
			// }
			// catch (IOException e1){
			// }

			JFileChooser chooser = new JFileChooser();
			int status = chooser.showOpenDialog(null);
			if (status == JFileChooser.APPROVE_OPTION) {
				File file = chooser.getSelectedFile();
				if (file == null) {
					return;
				}
				String fileName = chooser.getSelectedFile().getAbsolutePath();
				// load CSV
				CSVLoader loader = new CSVLoader();
				try {
					loader.setSource(new File(fileName));
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				try {
					data = loader.getDataSet();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

				// setting class attribute
				data.setClassIndex(data.numAttributes() - 1);
				// fc = new FilteredClassifier();

				m_View.setInstances(data);// Set instances to relationshipview
			}

		}

	}

	class ReloadButtonListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			m_View.setInstances(data);
		}

	}

	class AttributeSelectionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			try {
				final javax.swing.JFrame jf = new javax.swing.JFrame(
						"Select attributes");
				jf.getContentPane().setLayout(new BorderLayout());
				weka.gui.explorer.AttributeSelectionPanel sp = new weka.gui.explorer.AttributeSelectionPanel();
				jf.getContentPane().add(sp, BorderLayout.CENTER);
				weka.gui.LogPanel lp = new weka.gui.LogPanel();
				sp.setLog(lp);
				jf.getContentPane().add(lp, BorderLayout.SOUTH);
				jf.addWindowListener(new java.awt.event.WindowAdapter() {
					@Override
					public void windowClosing(java.awt.event.WindowEvent e) {
						jf.dispose();
//						System.exit(0);
					}
				});
				jf.pack();
				jf.setVisible(true);

				Instances i = new Instances(data);
				sp.setInstances(i);

			} catch (Exception ex) {
				ex.printStackTrace();
				System.err.println(ex.getMessage());
			}
		}
	}

}
